package com.itheima;
import org.dom4j.Attribute;
import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.Element;
import org.dom4j.io.SAXReader;

import java.io.File;
import java.util.ArrayList;
import java.util.List;

public class Xmlparse {
    public static void main(String[] args) throws DocumentException {
        SAXReader s=new SAXReader();
        Document document = s.read(new File("myXML\\src\\com\\itheima\\student.xml"));
        Element rootElement = document.getRootElement();
        List<Element> elementstudent = rootElement.elements("student");
        List<Student> list=new ArrayList<>();
        for (Element element : elementstudent) {
            Attribute attribute = element.attribute("id");
            String id = attribute.getValue();
            Element nameElement = element.element("name");
            String name = nameElement.getText();
            Element ageElement = element.element("age");
            String age = ageElement.getText();
            Student student=new Student(id,name,Integer.parseInt(age));
            list.add(student);
        }

        for (Student student : list) {
            System.out.println(student);
        }
    }
}
